﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;
using System.IO;
using System.Web.UI.HtmlControls;

namespace VSM
{
    public partial class MRPList : System.Web.UI.Page
    {
        private static string FromDocDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("dd/MM/yyyy");
        private static string ToDocDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month)).ToString("dd/MM/yyyy");
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Utils.AppConstants.ID_User] == null
                || Session[Utils.AppConstants.Password] == null
                || Session[Utils.AppConstants.ID_Dealer] == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    txtFromDocDate.Text = FromDocDate;
                    txtToDocDate.Text = ToDocDate;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            LoadData();
            Timer1.Enabled = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdBP_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                DataTable dt = Session["BPTable"] as DataTable;
                if (dt != null)
                {
                    dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
                    Session["BPTable"] = dt.DefaultView.ToTable();
                    this.grdBP.DataSource = dt.DefaultView.ToTable();
                    this.grdBP.DataBind();
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + ex.ToString() + "','');", true);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="column"></param>
        /// <returns></returns>
        private string GetSortDirection(string column)
        {
            string sortDirection = "ASC";
            string sortExpression = ViewState["SortExpression"] as string;

            if (sortExpression != null)
            {
                if (sortExpression == column)
                {
                    string lastDirection = ViewState["SortDirection"] as string;
                    if ((lastDirection != null) && (lastDirection == "ASC"))
                    {
                        sortDirection = "DESC";
                    }
                }
            }

            ViewState["SortDirection"] = sortDirection;
            ViewState["SortExpression"] = column;

            return sortDirection;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdBP_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");
                    e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

                    e.Row.Attributes["style"] = "cursor:pointer";

                    string id = this.grdBP.DataKeys[e.Row.RowIndex].Value.ToString();
                    GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;

                    DataSet dsDetail = (DataSet)Session["DocMRPListTableDetail"];
                    if (dsDetail != null && dsDetail.Tables.Count > 0)
                    {
                        DataRow[] rows = dsDetail.Tables[0].Select("HeaderID=" + id + "");
                        if (rows.Length > 0)
                        {
                            DataTable tbDetail = dsDetail.Tables[0].Clone();
                            foreach (DataRow row in rows)
                            {
                                tbDetail.ImportRow(row);
                            }
                            gvOrders.DataSource = tbDetail;
                            gvOrders.DataBind();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + ex.ToString() + "','');", true);
            }
        }
         /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvOrder_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");
                    e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

                    e.Row.Attributes["style"] = "cursor:pointer";
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + ex.ToString() + "','');", true);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFiletrRemark_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable tblDocList = (DataTable)Session["DocMRPListTable"];
                if (tblDocList != null && this.txtRemark.Text.Trim().Length > 0)
                {
                    DataView dv = tblDocList.DefaultView;
                    if (!string.IsNullOrEmpty(this.txtRemark.Text.Trim()))
                        dv.RowFilter = "Comments like '%" + this.txtRemark.Text.Trim() + "%'";
                    else
                        dv.RowFilter = "";

                    this.grdBP.DataSource = dv.ToTable();
                    this.grdBP.DataBind();
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + ex.ToString() + "','');", true);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            FromDocDate = txtFromDocDate.Text;
            ToDocDate = txtToDocDate.Text;
            LoadData();
            btnFiletrRemark_Click(null, null);
        }

        /// <summary>
        /// 
        /// </summary>
        private void LoadData()
        {
            try
            {
                MasterData obj = new MasterData();
                string dbType = Session[Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;

                DateTime fromDocDate = Utils.AppConstants.ConvertToDate((string.IsNullOrEmpty(txtFromDocDate.Text) ? "01/01/1900" : txtFromDocDate.Text));
                DateTime toDocDate = Utils.AppConstants.ConvertToDate((string.IsNullOrEmpty(txtToDocDate.Text) ? "31/12/9998" : txtToDocDate.Text));

                DataSet dsDocList = obj.GetMRPList(dbType, Session[Utils.AppConstants.ID_Dealer].ToString(), fromDocDate, toDocDate, txtRemark.Text.Trim(), txtSName.Text.Trim());
                DataSet dsDocListDetail = obj.GetMRPDetailList(dbType, Session[Utils.AppConstants.ID_Dealer].ToString());
                Session["DocMRPListTableDetail"] = dsDocListDetail;

                if (dsDocList != null && dsDocList.Tables != null)
                    BindDocList(dsDocList.Tables[0]);
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + ex.ToString() + "','');", true);
            }
        }
        /// <summary>
        /// Event Handler for Group Column.//
        /// </summary>
        /// <param name="sender">sender control</param>
        /// <param name="e">Event Args</param>
        protected void ddlGroupColumn_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tb"></param>
        private void BindDocList(DataTable tb)
        {
            Session["DocMRPListTable"] = tb;
            this.grdBP.DataSource = tb;
            this.grdBP.DataBind();
        }

        protected void grdBP_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            DataTable tblBP = (DataTable)Session["DocMRPListTable"];
            this.grdBP.PageIndex = e.NewPageIndex;
            BindDocList(tblBP);
        }
    }
}